package com.example.user.controller;


import com.example.user.entity.UcenterMember;
import com.example.user.entity.voco.LoginVo;
import com.example.user.entity.voco.RegisterVo;
import com.example.user.service.UcenterMemberService;
import com.example.user.utils.JwtUtils;
import com.example.utils.Result;
import com.example.utils.order.OrderMember;

import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-13
 */
@RestController
@RequestMapping("/cms/user")
@CrossOrigin
public class UcenterMemberController {
    @Autowired
    UcenterMemberService ucenterMemberService;

    @PostMapping("login")
    public Result loginAccount(@RequestBody LoginVo loginVo){
        String token = ucenterMemberService.login(loginVo);
        return Result.ok().data("token",token);
    }

    @PostMapping("register")
    public Result loginAccount(@RequestBody RegisterVo registerVo){
        Boolean flag = ucenterMemberService.register(registerVo);
        return Result.ok();
    }

    @GetMapping("auth/getLoginInfo")
    public Result getLoginInfo(HttpServletRequest req){
        String memberId = JwtUtils.getMemberIdByJwtToken(req);
        UcenterMember ucenterMember = ucenterMemberService.getById(memberId);
        System.out.println(ucenterMember);
        return Result.ok().data("memeber", ucenterMember);
    }

    @GetMapping("getMember/{id}")
    public UcenterMember getMemberById(@PathVariable String id){
        UcenterMember ucMember = ucenterMemberService.getById(id);
        return ucMember;
    }
    
    @GetMapping("getOrderMember/{memberId}")
    public OrderMember getOrderMember(@PathVariable String memberId){
        UcenterMember ucMember = ucenterMemberService.getById(memberId);
        OrderMember orderMember = new OrderMember();
        BeanUtils.copyProperties(ucMember, orderMember);
        return orderMember;
    }

    @GetMapping("getRegisterNumByDay/{day}")
    public Integer getRegisterNumByDay(@PathVariable String day){
        Integer registerNum = ucenterMemberService.getRegistersByDay(day);
        return registerNum;
    }




}

